package com.tarim.streetdance.api;

import com.tarim.streetdance.model.Type;
import com.tarim.streetdance.service.TypeService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/type")
public class TypeApi {
    @Autowired
    private TypeService typeService;

    /**
     * 查询全部视频类别
     * @param type
     * @return
     */
    @RequestMapping("/selectAll")
    public JsonResult selectAll (Type type) {
        JsonResult jsonResult;
        try {
            List<Type> list = typeService.selectAll(type);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 分页查询视频类别
     * @param pageIndex
     * @param pageSize
     * @param typeName
     * @return
     */
    @RequestMapping("/selectWithPageBean")
    public JsonResult selectWithPageBean (@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "typeName") String typeName) {
        JsonResult jsonResult;
        try {
            PageBean<Type> pageBean = new PageBean<Type>(pageIndex, pageSize);
            Type type = new Type();
            type.setTypeName(typeName);
            pageBean = typeService.selectWithPageBean(pageBean, type);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", pageBean);
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常，请联系管理员");
        }
        return jsonResult;
    }
}
